package com.huluobo.a2109mvpimdemo.login;

import com.huluobo.a2109mvpimdemo.IMApp;
import com.huluobo.a2109mvpimdemo.base.BasePresenter;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

/**
 * Created by LC on 2023/11/10.
 */
public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter {
    @Override
    public void login(String username, String password) {
        if (isAttach()) {
            //用户名去匹配正则
            if (username.matches(IMApp.REGEX_USERNAME)) {
                if (password.matches(IMApp.REGEX_PASSWORD)) {
                    getView().onStartLogin();//在这里触发登录的进度条
                    //登录到环信
                    LoginEMClient(username, password);

                } else {
                    getView().onPasswordError();
                }
            } else {
                getView().onUsernameError();
            }
        }
    }

    //这个方法就相当于是M层,因为没有bean类返回,所以用一个方法代替
    private void LoginEMClient(String username, String password) {
        EMClient.getInstance().login(username, password, new EMCallBack() {
            @Override
            public void onSuccess() {
                getView().onLoginSuccess();
            }

            @Override
            public void onError(int code, String error) {
                getView().onLoginFailed(error);
            }
        });
    }
}
